package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JComboBox;

import client.CcrmApp;
import model.Envelope;
import model.SalesReportModel;
import view.AreaGui;
import view.SalesReportGui;
/**
 * This Class is a Sales Report Controller, which extends AbstractTransfer
 * @author jacob
 *
 */
public class SalesReportController extends AbstractTransfer {
	private SalesReportGui ChsaleView;
	private MarketingManagController prevComt;
	private ArrayList<Integer> ar;
	private SalesReportController currCon;
	private int idIndex = 0;
	private boolean flagFound = true;
	private ArrayList<Integer> SalesR;
	/**
	 * Constructor
	 * @param ChsaleView is Sales Report Gui
	 * @param MarkManCon Marketing Manager Controller
	 */
	
	public SalesReportController(SalesReportGui ChsaleView,MarketingManagController MarkManCon ){
		this.ChsaleView = ChsaleView;
		this.prevComt = MarkManCon;
		currCon = this;
		//add Action Listener to gui buttons
		ChsaleView.addBackActionListener(new BackBottonListener());
		ChsaleView.addBackBottonSalesreportActionListener(new BackSalessReportListener());
		ChsaleView.addNextBottonActionListener(new NextListener());
		initCampainDetails();
		
		ChsaleView.addIdSalesActionListener(new SalesMarkItemListener());
	}
	
	/**
	 * this function get the Campaign Details and put this in combo box
	 */
	public void initCampainDetails(){
          ar = new ArrayList<Integer>();
		Envelope ev1 = new Envelope(ar,"serch campaign");
		CcrmApp.clien.setCurrObj(currCon);
		sendToServer(ev1);
	}
	/**
	 * Inner class that handles when some item on combo box  Pressed, implements ItemListener	
	 * @author jacob
	 *
	 */
	
	class SalesMarkItemListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			if(flagFound){ //flagFound means if there is some Campaign to show
			JComboBox Combo = ChsaleView.getSalesCombo();
			String st = (String) Combo.getSelectedItem();
           String[] tokens = st.trim().split("\\s+");
           idIndex = Integer.parseInt(tokens[2]); //save the id Campaign to send to server
		}
		}
		
	}
	/**
	 * Inner class that handles when Button back Pressed, implements ActiontListener	
	 * @author jacob
	 *
	 */
	class BackBottonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//show the previous gui
			ChsaleView.dispose();
			prevComt.MarkManGui.setVisible(true);		
		}
	}
	/**
	 * Inner class that handles when Button back in sales gui report(two panel on one gui - two back botton) Pressed, implements ActiontListener	
	 * @author jacob
	 *
	 */
	class BackSalessReportListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//initCampainDetails();
			ChsaleView.dispose();
			prevComt.MarkManGui.setVisible(true);
				
		}
	}
	/**
	 * Inner class that handles when Button next Pressed, implements ActiontListener	
	 * @author jacob
	 *
	 */
	class NextListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(flagFound){ //if their is Active campain
				SalesR = new ArrayList<Integer>();
				SalesR.add(idIndex);
				Envelope ev2 = new Envelope(SalesR, "serch Sales report");
				// send to server and search details on Sales report
				CcrmApp.clien.setCurrObj(currCon);
				sendToServer(ev2);
				ChsaleView.dispose();		
			}
				
		}
	}

	 @SuppressWarnings("unchecked")
	 @Override
	 /*
	  * (non-Javadoc)
	  * @see controllers.AbstractTransfer#handleDBResult(java.lang.Object)
	  */
	public void handleDBResult(Object message) {
		 Envelope ev = (Envelope)message;
		 if(ev.getTask().equals("serch campaign")){
		  if(ev.getMess().equals("no campaign")){
			 ChsaleView.setWarnningMTrue(); //set Warning that no campaign to show
			 flagFound = false;
		  }
		  else{
			 flagFound = true;
			 //add to combo box the campaignId 
			for(int i = 0; i < ((ArrayList<Integer>)ev.getObject()).size() ; i++){
				String str = "Campaign number: " + Integer.toString(((ArrayList<Integer>)ev.getObject()).get(i));
				ChsaleView.getSalesCombo().addItem(str);
			}
		    
		    }
		 }
		 //make the Sales report 
		 if(ev.getTask().equals("serch Sales report")){
			String report = "";
			report+="Report - Marketing sales no:  "+Integer.toString(((ArrayList<Integer>)ev.getObject()).get(0)) + "\n";
			report+="Number of Sales event:  "+Integer.toString(((ArrayList<Integer>)ev.getObject()).get(1)) + "\n";
			report+="Number of potinial customer: "+Integer.toString(((ArrayList<Integer>)ev.getObject()).get(2)) + "\n";
			report+="Sum of time calls:  "+Integer.toString(((ArrayList<Integer>)ev.getObject()).get(3)) + "\n\n";
			report+="Negative comments: "+"\n"+((String)ev.getMess());
			ChsaleView.setStr(report);
		     ChsaleView.initSalesRep();
		     ChsaleView.getSalesReportPanel().setVisible(true);
		     
		     
		 }
		 
	 }
}
